﻿using log4net;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Timesheet.Data.Infrastructure;
using Timesheet.Data.Repository;
using Timesheet.Domain;

namespace Timesheet.Service
{
    public class PrjReferencesService : IPrjReferencesService
    {
        private readonly IPrjReferencesRepository _Repository;
        private readonly ILocationsRepository _LocationRespository;
        private readonly IUnitOfWork _UnitOfWork;
        public ILog Logger { get; private set; }

        public PrjReferencesService(ILocationsRepository locationRespository,IPrjReferencesRepository PrjReferencesRepository, IUnitOfWork unitOfWork)
        {
            this._Repository = PrjReferencesRepository;
            this._UnitOfWork = unitOfWork;
            this._LocationRespository = locationRespository;
        }


        #region IPrjReferencesService Members


        public PrjReferences GetByID(long id)
        {
            return _Repository.GetById(id);
        }

        public void Add(PrjReferences entity)
        {
            if (entity.Type == "2")//Coordinator
            {
                _Repository.Add(entity);
                return;
            }
            ILocationsRepository locationRes = new LocationsRepository(this._UnitOfWork);
            locationRes.Add(new Locations()
            {
                Active = true,
                Client = entity.ProjID + entity.RefNames,
                LocationName = entity.RefNames
            });
            var prevLoc = locationRes.GetAll().FirstOrDefault(x => x.Client == entity.ProjID + entity.RefNames);
            if (prevLoc != null)
            {
                entity.RefID = prevLoc.Id;
                _Repository.Add(entity);
            }
        }

        public bool Update(PrjReferences entity)
        {
            PrjReferences exited = _Repository.GetQuery().FirstOrDefault(x => x.ProjID == entity.ProjID && x.RefID == entity.RefID && x.Id != entity.Id);
            if (exited != null)
                return false;
            entity.RefNames = entity.RefNames + "";
            _Repository.Update(entity);
            return true;
        }

        public bool CoordUpdate(PrjReferences entity)
        {
            PrjReferences exited = _Repository.GetQuery().FirstOrDefault(x => x.ProjID == entity.ProjID && x.RefID == entity.RefID && x.Id != entity.Id);
            if (exited != null)
                return false;
            PrjReferences domain = _Repository.GetById(entity.Id);
            domain.RefNames = entity.RefNames;
            domain.RefID = entity.RefID;
            domain.Active = entity.Active;
            _Repository.Update(domain);
            return true;
        }

        public bool LocationUpdate(PrjReferences entity)
        {
            PrjReferences domain = _Repository.GetById(entity.Id);
            domain.RefNames = entity.RefNames;
            domain.Active = entity.Active;
            _Repository.Update(domain);
            return true;
        }
        

        public void Delete(PrjReferences entity)
        {
            _Repository.Delete(entity);
            if(entity.Type=="1")
            {
                _LocationRespository.Delete(entity.RefID);
            }
        }

        public IList<PrjReferences> GetPrjReferences(string type, long prjId)
        {
            if(type.Equals("1"))
                return _Repository.GetQuery().Where(x => x.Type == type && x.ProjID == prjId).OrderBy(x=>x.RefNames).ToList();
            return _Repository.GetQuery().Where(x => x.Type == type && x.ProjID == prjId).OrderBy(x=>x.RefID).ToList();
        }

        public PrjReferences GetPrjReference(string type,long id)
        {
           return _Repository.GetQuery().Where(x => x.Type == type && x.RefID == id).FirstOrDefault();
        }

        #endregion


        public PrjReferences GetByRefId(long p)
        {
            PrjReferences exited = _Repository.GetQuery().FirstOrDefault(x =>  x.RefID == p );
            return exited;
        }
    }
}
